package cn.hp.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hp.dao.UserDao;
import cn.hp.domain.Role;
import cn.hp.domain.SysUser;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private PasswordEncoder passwordEncoder;
	
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		//根据用户名获取(SysUser)对象
		SysUser sysUser=userDao.findByUsername(username);
		if(sysUser!=null){
			//创建角色集合对象  因为一个用户可以拥有多个角色    
			Collection<GrantedAuthority> authorities=new ArrayList<GrantedAuthority>();
			//创建角色对象, 
			//GrantedAuthority grantedAuthority=new SimpleGrantedAuthority("ROLE_USER");
			//把角色对象放到角色集合里面
			//authorities.add(grantedAuthority);
			
			
			for (Role role : sysUser.getRoleList()) {
				GrantedAuthority grantedAuthority=new SimpleGrantedAuthority("ROLE_"+role.getRoleName());
				authorities.add(grantedAuthority);
			}
			
			
			
			//安全框架自带的用户对象
			User user=new User(sysUser.getUsername(),sysUser.getPassword(), authorities); 
			return user; 
		}
		return null;
	}

	//查询所有用户
	@Override
	public List<SysUser> findAll() {
		
		return userDao.findAll();
	}
	
	
	//添加用户
	@Override
	public void save(SysUser sysUser) {
		String password=sysUser.getPassword();
		String encode = passwordEncoder.encode(password);
		sysUser.setPassword(encode);
		userDao.save(sysUser);
	}
	
	
	//判断用户名唯一性
	@Override
	public Boolean uniqueName(String username) {
		SysUser sysUser=userDao.uniqueName(username);
		//如果sysUser为空,用户名可以使用,返回true
		//如果sysUser不为空,用户名不可以使用,返回false
		
		return sysUser==null;
	}
	
	
	//根据ID查询客户信息
	@Override
	public SysUser findById(Integer id) {
		return userDao.findById(id);
	}
	
	
	//根据id查询角色和权限信息
	@Override
	public List<Role> findRolePermission(Integer id) {
		return userDao.findRolePermission(id);
	}
	
	
	//根据ID查询用户已经拥有的角色
	@Override
	public List<Role> findUserRole(Integer id) {
		return userDao.findUserRole(id);
	}
	
	
	//给用户添加角色
	@Override
	public void addRoleToUser(Integer id, Integer[] ids) {
		//先清空所有的角色
		userDao.delete(id);
		//维护新的关系
		//判断ids是否为null
		if(ids!=null){
			for (Integer rid : ids) {
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("id", id);
				map.put("rid", rid); 
				userDao.addRoleToUser(map);
			}
		}
		
		
	}
	
	
	
	
	

}
